/**
 * 
 */
package com.oolong.account.web;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.oolong.account.model.OutItemModel;
import com.oolong.account.model.OutTypeModel;
import com.oolong.account.model.UserModel;
import com.oolong.account.model.factory.TypeFactory;
import com.oolong.account.service.ExtraService;
import com.oolong.account.service.OutManage;
import com.oolong.account.service.StaticManage;
import com.oolong.account.util.ParamterCheck;

/**
 * @author oolong
 * 
 */
// 标记它是个SPRING的BEAN，是MVC里的C
@Controller
// 表示这个类就是insertOutItem.do
@RequestMapping("/insertOut.do")
public class OutInsertController {
	Logger logger = Logger.getLogger(StaticDayController.class);
	// 表示需要SPRING自动注入
	@Autowired
	private OutManage outManage;
	@Autowired
	private TypeFactory typeFactory;
	String message = " ";
	@Autowired
	private LoadGlobalParameter loadGlobalParameter;
	@Autowired
	private ExtraService extraService;
	@Autowired
	private StaticManage staticManage;

	@RequestMapping(method = RequestMethod.POST)
	public String processOutItem(@RequestParam("money") String money,
			@RequestParam("sontype") String sontype,
			@RequestParam("memo") String memo, HttpServletResponse response,
			HttpServletRequest request, HttpSession session, ModelMap model) {
		response.setContentType("text/html;charset=UTF-8");
		UserModel userModel = (UserModel) session.getAttribute("user");
		OutItemModel outItemModel = new OutItemModel();
		// 设置金额
		BigDecimal bigmoney = null;
		try {
			bigmoney = new BigDecimal(money);
			outItemModel.setMoney(bigmoney);
		} catch (NumberFormatException e) {
			message = "传入的金额不正确";
			model.addAttribute("message", message);
			return "deleteOutItem";
		}
		// 设置备注
		if (ParamterCheck.checkMemo(memo)) {
			outItemModel.setMemo(memo);
		} else {
			message = "传入的备注信息不正确，不得大于128个字符";
			model.addAttribute("message", message);
			return "deleteOutItem";
		}
		// 设置支出类型
		int sonid = Integer.valueOf(sontype).intValue();
		if (!ParamterCheck.towTypeCheck(sonid)) {
			message = "传入的支出类型不正确，请不要篡改为不存在的支出类型";
			model.addAttribute("message", message);
			return "deleteOutItem";
		}
		OutTypeModel outTypeModel = typeFactory.getIOutTypeModel(sonid);
		outItemModel.setUser(userModel);
		outItemModel.setOutType(outTypeModel);
		int insertresult = outManage.add(outItemModel);
		model.addAttribute("outfathertypes",
				loadGlobalParameter.globaloutfathertypes);
		model.addAttribute("outsontypesList",
				loadGlobalParameter.globaloutsontypes);
		if (insertresult > 0) {
			String text = "亲爱的"
					+ userModel.getName()
					+ ":\n"
					+ "   您已经记录一条支出记录，记录的信息为："
					+ outTypeModel.getName()
					+ "-"
					+ outTypeModel.getOutTypeGroup().getName()
					+ "，支出金额"
					+ money
					+ "，备注为"
					+ memo
					+ "\n   您当前的帐户余额为："
					+ staticManage.TotalLastStaticService(userModel.getId())
							.getLastMoney();
			extraService.sendMail(userModel.getEmail(), text);
			message = "插入成功，ID为：" + insertresult;
			model.addAttribute("message", message);
			return "insertOutItem";
		} else {
			message = "插入失败，请联系管理员";
			model.addAttribute("message", message);
			return "deleteOutItem";
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	public String processOutItem(ModelMap model, HttpServletResponse response) {
		model.addAttribute("outfathertypes",
				loadGlobalParameter.globaloutfathertypes);
		model.addAttribute("outsontypesList",
				loadGlobalParameter.globaloutsontypes);
		message = " ";
		model.addAttribute("message", message);
		response.setContentType("text/html;charset=UTF-8");
		return "insertOutItem";
	}
}
